package com.sigerap.proveedor.model.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.sigerap.proveedor.model.Proveedor;
import com.sigerap.proveedor.model.dao.ProveedorDAO;
/**
 * 
 * @author Spirit-20121
 *
 */
public class JPAProveedorDAO implements ProveedorDAO {

	private EntityManager em;
	private Proveedor proveedor;

	public JPAProveedorDAO(EntityManager em) {
		this.em = em;
	}

	@Override
	public List<Proveedor> findAll() {
		Query query = em.createQuery("Select p.razonSocial From Proveedor p");
		return query.getResultList();
	}
	
	@Override
	public Proveedor findByRazon(Proveedor p){
		proveedor=em.find(Proveedor.class, p.getRazonSocial());
		return proveedor;
	}

	@Override
	public Proveedor findByNombre(String razonSocial) {
		proveedor = (Proveedor) em
				.createQuery(
						"Select p from Proveedor p where p.razonSocial =: razonSocial")
				.setParameter("razonSocial", razonSocial).getSingleResult();
		;
		return proveedor;
	}

	@Override
	public Proveedor insert(Proveedor p) {
		// TODO Auto-generated method stub
		em.persist(p);
		return p;
	}

}
